package com.tmy.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tmy.Dto.Result;
import com.tmy.entity.Collect;
import com.tmy.entity.Hotel;
import com.tmy.entity.RoomInfo;
import com.tmy.mapper.CollectMapper;
import com.tmy.mapper.HotelMapper;
import com.tmy.mapper.RoomInfoMapper;
import com.tmy.service.CollectService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Service
public class CollectServiceImpl implements CollectService {

    @Resource
    private CollectMapper collectMapper;
    @Resource
    public HotelMapper hotelMapper;
    @Resource
    private RoomInfoMapper roomInfoMapper;

    /**
     * 添加收藏
     */
    @Override
    public Result addCollect(Collect collect){
        Result result = new Result();
        try {
            LambdaQueryWrapper<Collect> lqw =new LambdaQueryWrapper<>();
            lqw.eq(Collect::getUserId,collect.getUserId());
            lqw.eq(Collect::getRoomId,collect.getRoomId());
            List<Collect> collectList = collectMapper.selectList(lqw);
            if (collectList.isEmpty()){
                collect.setCreateTime(new Date());
                collectMapper.insert(collect);
            }
            result.setCode(200);
            result.setMsg("收藏成功");
        }catch (Exception err){
            result.setCode(500);
            result.setMsg(err.getCause());
        }
        return result;
    }
    /**
     * 取消收藏
     */
    @Override
    public Result deleteCollect(Collect collect){
        Result result = new Result();
        try {
            if (!(collect.getId()==null)){
                collectMapper.deleteById(collect);
                result.setCode(200);
                result.setMsg("取消成功");
            }else if(!(collect.getUserId()==null)) {
                LambdaQueryWrapper<Collect> lqw =new LambdaQueryWrapper<>();
                lqw.eq(Collect::getUserId,collect.getUserId());
                lqw.eq(Collect::getHotelId,collect.getHotelId());
                lqw.eq(Collect::getRoomId,collect.getRoomId());
                collectMapper.delete(lqw);
                result.setCode(200);
                result.setMsg("取消成功");
            }else {
                result.setCode(400);
                result.setMsg("参数缺失");
            }
        }catch (Exception err){
            result.setCode(500);
            result.setMsg(err.getCause());
        }
        return result;
    }
    /**
     * 查询收藏
     */
    @Override
    public Result queryCollect(Collect collect){
        Result result = new Result();
        try {
            LambdaQueryWrapper<Collect> lqw= new LambdaQueryWrapper<>();
            lqw.eq(Collect::getUserId,collect.getUserId());
            List<Collect> collectList = collectMapper.selectList(lqw);
            for (Collect item:collectList){
                Hotel hotel = hotelMapper.selectById(item.getHotelId());
                RoomInfo roomInfo = roomInfoMapper.selectById(item.getRoomId());
                item.setHotelInfo(hotel);
                item.setRoomInfo(roomInfo);
            }
            result.setData(collectList);
            result.setCode(200);
            result.setMsg("收藏查询成功");
        }catch (Exception err){
            result.setCode(500);
            result.setMsg(err.getCause());
        }
        return result;
    }

}